<include file="Public:header_content" />

<div class="col-sm-12">
    <!-- Example Events -->

    <div class="example-wrap">
        <h4 class="example-title"></h4>
        <div class="example">
            <div class="btn-group hidden-xs" id="exampleTableEventsToolbar" role="group" style="float: left;margin-top: 10px;">

            </div>
            <table id="tableList"></table>
        </div>
    </div>
    <!-- End Example Events -->
</div>

<include file="Public:footer_content" />
<script src="/Public/plugin/gb312/js/bootstrap-table-edit.js"></script>
<script src="/Public/plugin/gb312/js/plugins/bootstrap-table/bootstrap-editable.js"></script>

<script type='text/javascript'>
    var tableList = $('#tableList');
    $(function () {
        tableList.bootstrapTable({
            url: "<{:U('Book/bookList')}>",
            refresh: {
                url: '<{:U("Book/bookList")}>',
                silent: false,
                //pageSize:2,
                query:{foo: 'bar'},
            },
            dataType:'json',
            method: 'post',
            contentType: "application/x-www-form-urlencoded",//post请求必带属性
            //sortClass: 'cate_id',
            cache:true,
            showRefresh: true,
            showToggle: true,
            showPaginationSwitch: true,//显示分页按钮开关
            buttonsAlign: 'left',
            toolbarAlign: 'left',
            //showHeader: false,
            showFooter: true,
            showColumns: true,
            //cardView: true,
            //detailView: true,//rue to show detail view table.
            sortStable: true,

            paginationPreText: '上一页',
            paginationNextText: '下一页',
            iconSize: 'outline',
            idField:'book_id',
            pagination: true, //分页
            dataField: 'lists',
            //pageNumber: '2',//初始化到第几页
            pageSize: 20,//初始化页面条数
            pageList:[10,20,50],
            singleSelect: false,
            //data-locale:"zh-US", //表格汉化
            search: true, //显示搜索框
            searchAlign: 'right',
            sidePagination: "server", //server 服务端处理分页
            columns: [{
                field: 'checkbox',
                title: '全选',
                checkbox: true
            },{
                field: 'book_id',
                title: 'ID',
                sortable: true,
                order:'desc',
                visible: false, //影藏该字段
            }, {
                field: 'book_name',
                title: '书名',
                formatter: function (value,row) {
                    return "<a class='' target='_blank' href='/"+row.book_id+".html' >"+value+"</a>"
                }
            },{
                field: 'status',
                title: '状态',
                formatter: function (value) {
                    switch(value){
                        case 'ing': return '更新中';
                        case 'end': return '已完结';
                        case 'other': return '其他';
                    }
                }
            },{
                field: 'author_id',
                title: '作者',
                formatter: function (value,row,index) {
                    return row.author_name;
                },
                events: function(e){
                    alert(e);
                }
            },{
                field: 'cate_id',
                title: '类型',
                formatter: function (value,row,index) {
                    return row.cate_name;
                }
            },{
                field: 'create_time',
                title: '添加时间',
                sortable: true,
                //clickToSelect: false,
                //switchable: false,
                align: 'center',
                valign: 'middle',
                formatter: function (value) {
                    return new Date(parseInt(value) * 1000).toLocaleString().substr(0,10).replace(/\//g, "-");
                    //return year+'-'+month+'-'+date;
                }
            },{
                field: 'id',
                title: '操作',
                align: 'center',
                valign: 'middle',
                formatter: function (value, row,index) {
                    var f = '<a href="__CONTROLLER__/editBook/book_id/'+row.book_id+'"  mce_href="#" on="editBook(\''+row.book_id+'\')"><i  class="fa fa-2x fa-edit"></i></a> ';
                    var e = '<a href="addArticle?book_id='+ row.book_id +'" ><i class="fa fa-2x fa-plus-square"></i></a> ';
                    var d = '<a href="#" mce_href="#" onclick="delBooks(\''+ row.book_id +'\')"><i class="glyphicon fa-2x glyphicon-trash"></i></a> ';
                    return f+e+d;
                }
            }],
            onClickRow: function(row,$element,field){
                $('.success').removeClass('success');
                $($element).addClass('success');
            },
            onDblClickCell: function(field, value, row, $element){
                  if(field == 'alias'){
                      //$element.empty();
                      //$element.append("<input type='text' name='alias' value='' />");
                      //return '<input type="text" name="alias" value="" />';
                  }
            }
        });

    });
    $('#add').click(function(){
        location.href="<{:U('Book/addArticle')}>";
    });

function delBooks(book_id){
    if(book_id > 0){
        swal({
            title: "您确定要删除该书",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false,
            cancelButtonText:'取消'
        }, function () {
            $.ajax({
                type: "POST",
                url: "__CONTROLLER__/delBooks",
                data: "book_id="+book_id,
                success: function(msg){
                  if(msg == 'success'){
                        tableList.bootstrapTable('remove',{
                            field: 'book_id',
                            values: [book_id]
                        });
                        swal("删除成功","","success");
                  }
                }
             });
        });
    }
    return false;
}

function editBook(book_id){
    if(book_id > 0){
        //iframe窗
        parent.layer.open({
            type: 2,
            title: '编辑书本信息',
            shade: [0.5],
            area: ['800px', '800px'],
            fix: false, //不固定
            skin: 'layui-layer-molv',
            content: ['__CONTROLLER__/editBook/book_id/'+book_id, 'no'], //iframe的url，no代表不显示滚动条
            end: function(){
                $('#tableList').bootstrapTable('refresh',{url: "<{:U('Book/bookList')}>"});
            }
        });
    }
}
</script>